﻿using System.Collections.Generic;
using System.Linq;
using Gumini.JulaDB.Data;
using Gumini.JulaDB.Engine;
using Gumini.JulaDB.Interfaces;

namespace Gumini.JulaDB.Planner
{
    /// <summary>
    /// Represents the CROSS JOIN operation.
    /// </summary>
    internal class CrossJoinNode : JoinNode
    {
        public CrossJoinNode(PlanNode nodeA, PlanNode nodeB)
            : base(nodeA, nodeB) { }

        public override List<Row> Execute()
        {
            int totalNumberOfColumns = LeftNode.Heading.NumberOfColumns + RightNode.Heading.NumberOfColumns;
            var result = new List<Row>();
            for (int i = 0; i < LeftNode.Rows.Count; i++)
            {
                for (int j = 0; j < RightNode.Rows.Count; j++)
                {
                    var newRow = new Row(LeftNode.Rows[i], RightNode.Rows[j]);
                    result.Add(newRow);
                }
            }
            return result;
        }

        protected override string GetNodeDescription()
        {
            return "JOIN (cartesian)";
        }
    }
}
